﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Configuration;
using System.ComponentModel.DataAnnotations;
using trangbaocao.Models;
using System.Collections.ObjectModel;
using System.Data.OracleClient;
namespace trangbaocao.Models
{
    public class ManufactorysModel
    {
        public int id { get; set; }
        [Required(ErrorMessage = "Tên nhà sản xuất không được để trống!")]
        public string name { get; set; }
        public string quoc_gia { get; set; }

        public List<ManufactorysModel> getNhasanxuat()
        {
            List<ManufactorysModel> user_ = new List<ManufactorysModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM manufactorys order by name");
            while (odr.Read())
            {
                user_.Add(new ManufactorysModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), quoc_gia = odr[2].ToString() });
            }
            return user_;
        }
        public List<ManufactorysModel> getNhasanxuatById(int id)
        {
            List<ManufactorysModel> user_ = new List<ManufactorysModel>();
            OracleDataReader odr = General.Lib_GetDataReader("SELECT * FROM manufactorys WHERE id=" + id);
            while (odr.Read())
            {
                user_.Add(new ManufactorysModel { id = Convert.ToInt32(odr[0].ToString()), name = odr[1].ToString(), quoc_gia = odr[2].ToString() });
            }
            return user_;
        }
        public void addNhasanxuat(ManufactorysModel nsx)
        {
            string sql = "INSERT INTO manufactorys";
            sql += " select nvl((select max(id)+1 from manufactorys),1),'" + nsx.name + "','" + nsx.quoc_gia + "' from dual";
            General.Lib_ExecuteNonQuery(sql);
        }

        public void updateNhasanxuat(ManufactorysModel nsx)
        {
            string sql = "UPDATE manufactorys SET name='" + nsx.name + "', quoc_gia='" + nsx.quoc_gia + "' where id=" + nsx.id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public void deleteNhasanxuat(int id)
        {
            string sql = "DELETE FROM manufactorys WHERE id=" + id;
            General.Lib_ExecuteNonQuery(sql);
        }
        public int checkExist(ManufactorysModel nsx)
        {
            string sql = "SELECT * FROM manufactorys WHERE name='" + nsx.name + "'";
            return General.Lib_CheckExist(sql);
        }
        public int checkSubId(int id)
        {
            string sql = "SELECT * FROM Device_Stations WHERE manufactorys_id='" + id + "'";
            return General.Lib_CheckExist(sql);
        }
    }
}